package colopllet.colopl.service.util;

import java.util.ArrayList;
import java.util.List;

import colopllet.colopl.pc.response.CheckBelongingSouvenirResponse;
import colopllet.colopl.pc.response.LinkColonyRes;
import colopllet.colopl.pc.response.LinkResponse;
import colopllet.colopl.pc.response.MyWarehouseItemRes;
import colopllet.colopl.pc.response.MyWarehouseResponse;
import colopllet.colopl.pc.response.SouvenirBelongingRes;
import colopllet.colopl.service.bean.ColonyLink;
import colopllet.colopl.service.bean.LinkType;
import colopllet.colopl.service.bean.SouvenirCount;
import colopllet.colopl.service.bean.WarehouseItem;

public class BeanUtil {

    public static List<WarehouseItem> createBeanFromResponse(
            List<MyWarehouseResponse> resList) {
        
        List<WarehouseItem> list = new ArrayList<WarehouseItem>();
        for (int i = 0; i < resList.size(); i++) {
            MyWarehouseResponse res = resList.get(i);
            List<MyWarehouseItemRes> itemList = res.getItems();
            for (int j = 0; j < itemList.size(); j++) {
                MyWarehouseItemRes itemRes = itemList.get(j);
                WarehouseItem item = new WarehouseItem();
                item.setAutoID(itemRes.getAutoID());
                item.setComment(itemRes.getComment());
                item.setLineNumber(j + 1);
                item.setName(itemRes.getName());
                item.setPageNumber(i + 1);
                item.setPrice(Integer.valueOf(itemRes.getPrice()));
                list.add(item);
            }
        }
        
        return list;
    }

    public static List<SouvenirCount> createBeanFromResponse(
            CheckBelongingSouvenirResponse res) {
        
        List<SouvenirCount> list = new ArrayList<SouvenirCount>();
        List<SouvenirBelongingRes> resList = res.getSouvenirBelongings();
        for (int i = 0; i < resList.size(); i++) {
            SouvenirBelongingRes souvenirRes = resList.get(i);
            SouvenirCount souvenir = new SouvenirCount();
            souvenir.setMyCount(Integer.valueOf(souvenirRes.getMyCount()));
            souvenir.setName(souvenirRes.getName());
            souvenir.setOmiyageID(Integer.valueOf(souvenirRes.getOmiyageID()));
            souvenir.setOtherCount(Integer.valueOf(souvenirRes.getOtherCount()));
            souvenir.setAreaID(i);
            list.add(souvenir);
        }
        
        return list;
    }

    public static List<ColonyLink> createBeanFromResponse(LinkResponse res) {
        List<ColonyLink> list = new ArrayList<ColonyLink>();
        list.addAll(getColonyLinks(res.getNearColony(), LinkType.NEAR));
        list.addAll(getColonyLinks(res.getFarColony(), LinkType.NEAR));
        list.addAll(getColonyLinks(res.getFriendColony(), LinkType.NEAR));
        
        return list;
    }
    
    private static List<ColonyLink> getColonyLinks(List<LinkColonyRes> list, LinkType type) {
        List<ColonyLink> links = new ArrayList<ColonyLink>();
        for (int i = 0; i < list.size(); i++) {
            LinkColonyRes near = list.get(i);
            ColonyLink link = new ColonyLink();
            link.setId(Integer.valueOf(near.getId()));
            link.setLinkType(LinkType.NEAR);
            link.setNickname(near.getNickname());
            links.add(link);
        }
        
        return links;
    }
    
}
